package com.stone.im.geteway.dispather;

import com.stone.im.common.Constants;
import com.stone.im.common.Request;
import com.stone.im.common.Response;
import com.stone.im.protobuf.proto.AuthenticateRequestProto;
import com.stone.im.protobuf.proto.MessagePushResponseProto;
import com.stone.im.protobuf.proto.MessageSendRequestProto;
import io.netty.channel.socket.SocketChannel;


public class DispatcherInstance {

    private SocketChannel socketChannel;

    public DispatcherInstance(SocketChannel socketChannel){
        this.socketChannel=socketChannel;
    }

    /**
     * 向分发系统发送认证请求
     * @param authenticateRequest
     * @return
     */
    public void authenticate(AuthenticateRequestProto.AuthenticateRequest authenticateRequest){

        Request request = new Request(Constants.APP_SDK_VERSION_1,
                Constants.REQUEST_TYPE_AUTHENTICATE,
                Constants.SEQUENCE_DEFAULT,
                authenticateRequest.toByteArray());
        System.out.println("发送消息");
        socketChannel.writeAndFlush(request.getBuffer());
    }

    /**
     * 向分发系统发送单聊消息
     * @param messageSendRequest
     */
    public void sendMessage(MessageSendRequestProto.MessageSendRequest messageSendRequest){
        Request request = new Request(Constants.APP_SDK_VERSION_1,
                Constants.REQUEST_TYPE_SEND_MESSAGE,
                Constants.SEQUENCE_DEFAULT,
                messageSendRequest.toByteArray());
        System.out.println("发送消息");
        socketChannel.writeAndFlush(request.getBuffer());
    }

    public void pushMessageResponse(MessagePushResponseProto.MessagePushResponse messagePushResponse){
        Response response = new Response(Constants.APP_SDK_VERSION_1,
                Constants.MESSAGE_TYPE_RESPONSE,
                Constants.REQUEST_TYPE_SEND_MESSAGE,
                Constants.SEQUENCE_DEFAULT,
                messagePushResponse.toByteArray());
        System.out.println("推送消息");
        socketChannel.writeAndFlush(response.getBuffer());
    }

    public SocketChannel getSocketChannel() {
        return socketChannel;
    }

    public void setSocketChannel(SocketChannel socketChannel) {
        this.socketChannel = socketChannel;
    }
}
